#include "../music_system.h"
#include "../../model/model_class.h"
#include "../../model/dom.h"
#include "../../network/xml_filetype.h"
#include "../../network/i_socket.h"
#include "../../network/tcp_socket.h"

#include <boost/shared_ptr.hpp>

#include <iostream>
#include <fstream>

#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
	srand(time(0));
	boost::shared_ptr<model::model_class> model(new model::model_class());
	music::music_system system(model);

	ifstream ifs("test.xml");
	string buff;
	string line;
	while(ifs.good()) {
		getline(ifs, line);
		buff += line;
	}
	cout << "parsing test.xml" << endl;
	network::xml_filetype ft;
	boost::shared_ptr<model::dom> n(ft.parse(buff));

	boost::shared_ptr<network::i_socket> sock(new network::tcp_socket());
	system.add_music(n, sock);
	cout << "client has connected" << endl;
	boost::shared_ptr<network::i_socket> s(
		system.play_music("test", "test.ogg"));
	if(s != sock) {
		cout << "error, s != sock" << endl;
		return 1;
	}
	cout << "playing test.ogg" << endl;
	s = system.play_music("test", "test2.ogg");
	if(s != sock) {
		cout << "error, s != sock" << endl;
		return 1;
	}
	cout << "playing test2.ogg" << endl;
	s = system.play_music("test", "test3.ogg");
	if(s != sock) {
		cout << "error, s != sock" << endl;
		return 1;
	}
	cout << "playing test3.ogg" << endl;
	cout << "suggesting title..." << endl;
	cout << system.suggest_title("test") << " suggested" << endl;
	cout << "disconnecting client test" << endl;
	system.remove_music(sock);
	cout << "test removed" << endl;
	cout << "trying to play test.ogg" << endl;
	try {
		s = system.play_music("test", "test2.ogg");
	} catch(std::runtime_error &err) {
		cout << "success" << endl;
		return 0;
	}
	return 1;
}

